package com.ecable.daoImpl;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ecable.bean.StateTO;
import com.ecable.daoI.AddStateDAOI;
import com.ecable.dao.AbstractDataAccessObject;

public class AddStateDAoImpl implements AddStateDAOI {
	PreparedStatement pstmt;
	Statement stmt;
	ResultSet rs;
	Connection con;

	public AddStateDAoImpl() {
		con = new AbstractDataAccessObject().getConnection();
	}

	public boolean insertState(StateTO st) {
		boolean flag = true;
		try {
			pstmt = con
					.prepareStatement("insert into state values((select nvl(max(stateid),100)+1 from state),?)");
			String sname = st.getSname();
			pstmt.setString(1, sname);
			int insert = pstmt.executeUpdate();
			if (insert > 0) {
				con.commit();
			} else {
				flag = false;
				con.rollback();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e);
			flag = false;
			try {
				con.rollback();
			} catch (SQLException se) {
				se.printStackTrace();
			}
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
		return flag;
	}

	public Vector<StateTO> viewState() {
		Vector<StateTO> vdf = null;
		try {
			pstmt = con.prepareStatement("select *from state");
			rs = pstmt.executeQuery();
			vdf = new Vector<StateTO>();
			while (rs.next()) {
				int sid = rs.getInt(1);
				String sname = rs.getString(2);
				StateTO sfb = new StateTO();
				sfb.setStateid(sid);
				sfb.setSname(sname);
				vdf.add(sfb);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return vdf;
	}

	public StateTO viewStateInfo(int sid1) {
		StateTO st = new StateTO();
		try {
			pstmt = con.prepareStatement("select *from state where stateid=?");
			pstmt.setInt(1, sid1);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				int sid = rs.getInt(1);
				String sname = rs.getString(2);
				st.setStateid(sid);
				st.setSname(sname);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return st;
	}

	public boolean deleteState(int sid) {
		boolean flag = true;
		try {
			pstmt = con.prepareStatement("delete from state where stateid=?");
			pstmt.setInt(1, sid);
			int delete = pstmt.executeUpdate();
			if (delete > 0) {
				con.commit();
			} else {
				flag = false;
				con.rollback();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			flag = false;
			try {
				con.rollback();
			} catch (SQLException se) {
				se.printStackTrace();
			}
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
		return flag;
	}

	public boolean updateState(StateTO df) {
		boolean flag = true;
		try {
			pstmt = con
					.prepareStatement("update state set statename=? where stateid=?");
			int sid = df.getStateid();
			System.out.println("in dao state id is........." + sid);
			String sname = df.getSname();
			System.out.println("in dao state name is ." + sname);
			pstmt.setString(1, sname);
			pstmt.setInt(2, sid);
			int update = pstmt.executeUpdate();
			if (update > 0) {
				con.commit();
			} else {
				flag = false;
				con.rollback();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			flag = false;
			try {
				con.rollback();
			} catch (SQLException se) {
				se.printStackTrace();
			}
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
		return flag;
	}
}
